1   // Copyright 2007, 2009 The Apache Software Foundation
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  
15  package org.apache.tapestry5.integration.pagelevel;
16  
17  import org.apache.tapestry5.dom.Document;
18  import org.apache.tapestry5.test.PageTester;
19  import org.testng.Assert;
20  import org.testng.annotations.DataProvider;
21  import org.testng.annotations.Test;
22  
23  public class DTDTest extends Assert
24  {
25      private static final String FRAMESET = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">";
26  
27      private static final String TRANSITIONAL = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
28  
29      private static final String STRICT = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">";
30  
31      @DataProvider
32      public Object[][] dtd_page_provider()
33      {
34          return new Object[][]
35                  {
36                          { "DTDFromPage", FRAMESET, "slagheap", },
37                          { "DTDFromComponent", TRANSITIONAL, "flubber", },
38                          { "MultipleDTD", STRICT, "blubber", },
39                          { "NoDTD", "", "no_dtd_loser", } };
40      }
41  
42      @Test(dataProvider = "dtd_page_provider")
43      public void verify_correct_dtds(String pageName, String expectedDTD, String checkText)
44      {
45          PageTester tester = new PageTester(TestConstants.APP2_PACKAGE, TestConstants.APP2_NAME);
46  
47          Document doc = tester.renderPage(pageName);
48          String txt = doc.toString();
49          // use startsWith to make sure the DTD is getting into the right spot.
50          assertTrue(txt.startsWith(expectedDTD));
51          // we should also make sure that the other DTD's don't appear anywhere else...
52          checkOtherDTD(txt, expectedDTD);
53          // spot check the body of the pages to make sure they correctly rendered...
54          // they should have, based on the unit tests for template rendering, but...
55          assertTrue(txt.contains(checkText));
56  
57      }
58  
59      private void checkOtherDTD(String txt, String expected)
60      {
61          if (expected.equals(TRANSITIONAL))
62          {
63              check(txt, FRAMESET, STRICT);
64          }
65          else if (expected.equals(FRAMESET))
66          {
67              check(txt, STRICT, TRANSITIONAL);
68              ;
69          }
70          else if (expected.equals(STRICT))
71          {
72              check(txt, FRAMESET, TRANSITIONAL);
73          }
74          else if (expected.equals(""))
75          {
76              check(txt, FRAMESET, STRICT, TRANSITIONAL);
77          }
78          else
79          {
80              throw new RuntimeException("Unknown expected string: " + expected);
81          }
82      }
83  
84      private void check(String txt, String... invalids)
85      {
86          for (String invalid : invalids)
87          {
88              assertFalse(txt.contains(invalid));
89          }
90      }
91  
92  }